import axios from 'axios'
import userUrl from '../../util/url'
let url = userUrl + 'api.php/detail'

const detail = {
    namespaced: true,
    state: {
        DetailData: [],
        ruleForm: {
            name: "",
            bianhao: "",
            leixing: "",
            guige: "",
            color: "",
            pinpai: "",
            price: "",
            caizhi: "",
            xinghao: '',
            type: '请选择',
            tese: '',
            youhui: ''
        },
        seleVal: [],
        seleId: [],
        total: 0,
        currentPage: 1,
        pageSize: 6,
    },
    mutations: {
        setCurrentPage(state, payload) {
            state.currentPage = payload
        },
        setSele(state, payload) {
            state.seleVal = payload
            payload.map((obj) => {
                state.seleId.push(obj.id)
            })
        },
        steForm(state, payload) {
            if (payload) {
                state.ruleForm = state.seleVal[0]
            } else {
                state.ruleForm = {
                    name: "",
                    bianhao: "",
                    leixing: "",
                    guige: "",
                    color: "",
                    pinpai: "",
                    price: "",
                    caizhi: "",
                    xinghao: '',
                    type: '',
                    tese: '',
                    youhui: ''
                }
            }

        }
    },
    actions: {
        getDetail({ state, commit, dispath }) { // 获取详情数据
            return new Promise((resolve, rejact) => {
                axios({
                    method: 'get',
                    url,
                    params: {
                        transform: 1,
                        order: 'id,desc',
                        page: state.currentPage + ',' + state.pageSize
                    }
                }).then(res => {
                    state.DetailData = res.data.detail
                    state.total = res.data._results
                    resolve()
                }).catch(() => {
                    rejact()
                })
            })
        },
        getAdd({ state, commit, dispatch }, payload) { //新增/修改
            return new Promise((resolve, rejact) => {
                let method = 'post',
                    url = userUrl + 'api.php/detail'

                if (payload) {
                    method = 'put'
                    url += '/' + state.seleVal[0].id
                }

                axios({
                    method,
                    url,
                    data: state.ruleForm
                }).then(res => {
                    dispatch('getDetail')
                    state.ruleForm = {
                        name: "",
                        bianhao: "",
                        leixing: "",
                        guige: "",
                        color: "",
                        pinpai: "",
                        price: "",
                        caizhi: "",
                        xinghao: '',
                        type: '',
                        tese: '',
                        youhui: ''
                    },
                        resolve()
                }).catch(() => {
                    rejact()
                })
            })
        },

        getRemove({ state, commit, dispatch }, payload) {
            return new Promise((resolve, rejact) => {
                axios({
                    method: 'delete',
                    url: url + '/' + payload,
                }).then(res => {
                    dispatch('getDetail')
                    resolve()
                }).catch(rej => {
                    rejact()
                })
            })
        },
        getSou({ state, commit, dispatch }, payload) {
            return new Promise((resolve, rejact) => {
                console.log(payload)
                axios({
                    method: 'get',
                    url,
                    params: {
                        transform: 1,
                        order: 'id,desc',
                        filter: 'name,cs,' + payload
                    }
                }).then(res => {
                    state.DetailData = res.data.detail
                    resolve()
                }).catch(rej => {
                    rejact()
                })
            })
        },
        
    }
}
export default detail